<?php
class BrandAction extends LoginAction
{
    protected $_type = 'brand';

    public function index()
    {
        $BrandModel = D('Brand');
        $SlideModel = M('Slide');
        $allCount = $BrandModel->count();
        $Page = $this->getPage($allCount);
        $brand = $BrandModel->getBrandAllPro(null, 'm_brand.id,m_brand.initials,m_brand.name,m_brand.ename,m_brand.small_image,m_brand.image,m_brand_pro.name as pName',
            $Page->firstRow . ',' . $Page->listRows, 'm_brand.id DESC');
        if ($brand) {
            foreach ($brand as $key => $b) {
                $smallImage = $b['small_image'];
                if ($smallImage) {
                    $smallImageUrl = $this->getManagedUrl($smallImage, $this->_type);
                    $brand[$key]['small_image_url'] = $smallImageUrl;
                }
                //获取宣传图数量
                unset($map);
                $map['bid'] = $b['id'];
                $map['type'] = 1;
                $brand[$key]['count'] = $SlideModel->where($map)->count(); //统计宣传图数量
            }
            $this->assign('brand', $brand);
            $this->assign('page', $Page->show());
        }
        $this->display();
    }

    /**
     * 添加品牌操作
     * @author Ice iceinto@mallog.com.cn
     */
    public function add()
    {
        //获取品牌属性
        $BrandProModel = M('BrandPro');
        if (isPost()) {
            $data = $this->getBrandPostData();
            $this->createAdd('Brand', $data);
        }
        //赋值设置
        $BrandPro = $BrandProModel->select();
        if ($BrandPro) {
            $this->assign('brandPro', $BrandPro);
        } else {
            $this->error('请先添加品牌属性！');
        }
        $this->display();
    }

    /**
     * 修改品牌操作
     * @author Ice iceinto@mallog.com.cn
     */
    public function edit()
    {
        //获取品牌属性和品牌数据
        $BrandProModel = M('BrandPro');
        $BrandModel = D('Brand');
        $id = (int)getGetOrPost('id');
        $map = array();
        $map['id'] = $id;
        $brand = $BrandModel->where($map)->find();
        if ($brand) {
            $this->assign('brand', $brand);
        } else {
            $this->error('不存在对应品牌信息！');
        }
        //判断数据提交
        if (isPost()) {
            $data = $this->getBrandPostData(true, $id);
            $data['id'] = $id;
            if ($this->createUpdate('Brand', $data, false)) {
                $this->successMessage('修改成功！');
                //修改成功删除原有图片
                if (($brand['b_image'] != $data['b_image']) &&
                    $data['b_image']
                ) {
                    $this->delUpLoadFile($brand['b_image'], 'brand');
                }
                //重新加载品牌信息
                $brand = $BrandModel->where($map)->find();
                $this->assign('brand', $brand);
            } else {
                $this->errorMessage('修改失败！');
            }
        }
        //加载品牌图片
        $imageUrl = $this->getManagedUrl($brand['image'], 'brand');
        $this->assign('imageUrl', $imageUrl);
        //赋值设置
        $BrandPro = $BrandProModel->select();
        if ($BrandPro) {
            $this->assign('brandPro', $BrandPro);
        } else {
            $this->error('请先添加品牌属性！');
        }
        $this->display();
    }

    /**
     * 小图管理
     * @author Ice iceinto@mallog.com.cn
     */
    public function small()
    {
        $id = intval(getGet('id'));
        $map = array();
        $BrandModel = M('Brand');
        $map['id'] = $id;
        $brand = $BrandModel->field('id,name,ename,small_image')->where($map)->find();
        if ($brand) {
            $this->assign('brand', $brand);
        } else {
            $this->error('不存在对应品牌信息！');
        }
        if (isPost()) {
            $data = array();
            $data['small_image'] = $this->uploadFiles($this->_type);
            $data['id'] = $id;
            $result = $BrandModel->save($data);
            if ($result) {
                $this->delUpLoadFile($brand['small_image'], $this->_type);
                $brand = $BrandModel->where($map)->find();
                $this->successMessage('修改成功！');
            } else {
                $this->error('修改失败！');
            }
        }
        $smallImage = $brand['small_image'];
        if ($smallImage) {
            $smallImageUrl = $this->getManagedUrl($smallImage, $this->_type);
            $this->assign('smallImageUrl', $smallImageUrl);
        }
        $this->display();
    }

    /**
     * 设置 小 png logo
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function png()
    {
        $id = intval(getGet('id'));
        $map = array();
        $BrandModel = M('Brand');
        $map['id'] = $id;
        $brand = $BrandModel->field('id,name,ename,png')->where($map)->find();
        if ($brand) {
            $this->assign('brand', $brand);
        } else {
            $this->error('不存在对应品牌信息！');
        }
        if (isPost()) {
            $data = array();
            $data['png'] = $this->uploadFiles($this->_type);
            $data['id'] = $id;
            $result = $BrandModel->save($data);
            if ($result) {
                $this->delUpLoadFile($brand['png'], $this->_type);
                $brand = $BrandModel->field('id,name,ename,png')->where($map)->find();
                $this->successMessage('修改成功！');
            } else {
                $this->error('修改失败！');
            }
        }
        if ($brand['png']) {
            $pngUrl = $this->getManagedUrl($brand['png'], $this->_type);
            $this->assign('png_url', $pngUrl);
        }
        $this->display();
    }

    /**
     * 执行品牌参数获取和判断
     * @param blooen $isUpload
     * @param blooen $isEdit
     * @author Ice iceinto@mallog.com.cn
     */
    private function getBrandPostData($isEdit = false, $id = 0)
    {
        $BrandProModel = M('BrandPro');
        $BrandModel = D('Brand');
        $brandPro = getPost('brandPro');
        $fName = trim(getPost('fName'));
        $eName = trim(getPost('eName'));
        $initials = strtoupper(getPost('initials'));
        //简介
        $brandIntr = getPost('brandIntr');
        // @TODO 如果插入失败执行删除文件操作
        $data = array();
        $data['pid'] = $brandPro;
        $data['name'] = $fName;
        $data['ename'] = $eName;
        $data['intr'] = $brandIntr;
        $data['initials'] = $initials;
        //文件执行上传并且保存返回媒体库标示
        $doUpload = true;
        if ($isEdit) {
            if (!$_FILES['image']['name']) {
                $doUpload = false;
            }
        }
        if ($doUpload) {
            $fId = $this->uploadFiles('brand');
            $data['image'] = $fId; //媒体库标示
            if (!$data['image']) {
                $this->error('图片传送失败');
            }
        }
        return $data;
    }

    public function search()
    {
        $keyword = trim($this->_get('keyword'));
        if (!empty($keyword)) {
            $map = array();
            $map['ename'] = array('LIKE', '%' . strtolower($keyword) . '%');
            $map['name'] = array('LIKE', '%' . $keyword . '%');
            $map['_logic'] = 'OR';
            $brands = M('brand')->field('id,name,ename')
                ->where($map)
                ->select();
            $this->assign('brands', $brands);
            $this->assign('keyword', $keyword);
        }
        $this->display();
    }
}

?>